package com.neusoft.controller;

import com.neusoft.po.CommonResult;
import com.neusoft.po.PointLog;
import com.neusoft.po.PointTrans;
import com.neusoft.service.PointService;
import com.neusoft.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RefreshScope
@RestController
@RequestMapping("/PointController")
public class PointController {

    @Autowired
    private PointService pointService;

    //  新建用户积分，初始积分+10
    @PostMapping("/savePoint/{userId}")
    public CommonResult<Integer> savePoint(
            @PathVariable("userId") String userId) throws Exception{
        PointLog pointLog = new PointLog();
        pointLog.setUserId(userId);
        pointLog.setTime(CommonUtil.getCurrentDate());
        int count = 10;
        for(int i = 1; i <= count; i++) {
            pointService.savePointLogByPay(pointLog);
        }
        return new CommonResult<>(200,"sucess",count);
    }

    //  用户增加count分
    @PostMapping("/savePointLogByPay/{userId}/{count}")
    public CommonResult<Integer> savePointLogByPay(
            @PathVariable("userId") String userId,
            @PathVariable("count") Integer count) throws Exception {
        PointLog pointLog = new PointLog();
        pointLog.setUserId(userId);
        pointLog.setTime(CommonUtil.getCurrentDate());
        for(int i = 1; i <= count; i++) {
            pointService.savePointLogByPay(pointLog);
        }
        return new CommonResult<>(200,"sucess",count);
    }

    //  查询当前时刻某用户的所有有效积分
    @GetMapping("/getPointLogByUserIdByTime/{userId}")
    public CommonResult<Integer> getPointLogByUserIdByTime(
            @PathVariable("userId") String userId) throws Exception {
        int count = pointService.getPointLogByUserIdByTime(userId);
        return new CommonResult<>(200,"sucess",count);
    }

    //  某个用户用掉count分
    @DeleteMapping("/deletePointLogByUserIdByTime/{userId}/{count}")
    public CommonResult<Integer> deletePointLogByUserIdByTime(
            @PathVariable("userId") String userId,
            @PathVariable("count") Integer count) throws Exception {
        int isOk = pointService.deletePointLogByUserIdByTime(userId,count);
        return new CommonResult<>(200,"sucess",isOk);
    }

    //  删去某个用户已过期的积分
    @DeleteMapping("/updatePointLog/{userId}")
    public CommonResult<Integer> updatePointLog(
            @PathVariable("userId") String userId) throws Exception {
        int count = pointService.updatePointLog(userId);
        return new CommonResult<>(200,"sucess",count);
    }

    //  查询某个用户快过期的积分
    @GetMapping("/getExpirationPointLogByUserId/{userId}")
    public CommonResult<Integer> getExpirationPointLogByUserId(
            @PathVariable("userId") String userId) throws Exception {
        int count = pointService.getExpirationPointLogByUserId(userId);
        return new CommonResult<>(200,"sucess",count);
    }

    @GetMapping("/listPointTransByUserId/{userId}")
    public CommonResult<List> listPointTransByUserId(
            @PathVariable("userId") String userId) throws Exception {
        List<PointTrans> list = pointService.listPointTransByUserId(userId);
        return new CommonResult<>(200,"sucess",list);
    }

    @PostMapping("/savePointTrans/{userId}/{type}/{count}")
    public CommonResult<Integer> savePointTrans(
            @PathVariable("userId") String userId,
            @PathVariable("type") String type,
            @PathVariable("count") BigDecimal count) throws Exception {
        PointTrans pointTrans = new PointTrans();
        pointTrans.setUserId(userId);
        pointTrans.setType(type);
        pointTrans.setCount(count);
        pointTrans.setTime(CommonUtil.getCurrentDate());
        int isOk = pointService.savePointTrans(pointTrans);
        return new CommonResult<>(200,"sucess",isOk);
    }

}
